import api from '../api'
import utils from '@/assets/js/utils'
import env from '@/assets/env'
const state = {
  features: [],
  featureList: [], // 一级标签 新
  iconList: [],
  activeIcon: null,
  editFeature: null,
  markDatas: []
}

const getters = {
  featureList: state => state.featureList,
  markDatas: (state, getters) => {
    // let list = state.markDatas.filter(t => t._del !== true);
    // let tab = getters.selectTab;
    // if (tab == null) return list;
    // return list.filter(t => t.landmarkMark_type == tab.id);

    // new
    let arr = getters.featureList;
    let list = arr.filter(t => !t.marks || t.marks.length == 0)
    let tab = getters.selectTab;
    if (tab == null) return list;
    return list.filter(t => t.landmarkMark_type == tab.id)
  },
  editFeature: state => state.editFeature,
  editFeatureTab: (state, getters) => {
    if (getters.editFeature == null) return null
    let feature = getters.editFeature
    let tab = getters.allTabList.find(t => t.id == feature.landmarkMark_type)
    return tab;
  },
  features: state => state.features,
  newFeatures: (state, getters) => {
    // let arr = state.features.map(t => {
    //   return utils.getMarker(t);
    // })
    // return arr;

    // new
    let list = getters.featureList.filter(t => t.marks && t.marks.length > 0)
    let arr = list.map(t => {
      let mark = t.marks[0];
      let feature = utils.getMarker(mark);
      return {
        feature,
        data: t
      }
    })
    return arr;
  },
  noPointFeatures: (state, getters) => {
    return getters.features.filter(t => {
      return !utils.hasPoint(t);
    })
  },
  hasPointFeatures: (state, getters) => {
    return getters.features.filter(t => {
      return utils.hasPoint(t)
    });
  },
  iconList: state => state.iconList,
  activeIcon: (state, getters) => {
    let iconList = getters.selectTabIconList
    if (iconList.length == 0) return null;
    if (state.activeIcon == null) return iconList[0];
    let item = iconList.find(t => t.id == state.activeIcon.id);
    if (item) return item;
    return iconList[0];
  },
  iconImageUrl: (state, getters) => {
    const defaultUrl = LOCAL_ROOT + 'static/image/point.png'
    if (getters.activeIcon == null) return defaultUrl
    if (!getters.activeIcon.normal) return defaultUrl
    return env.FILE_URL + getters.activeIcon.normal
  },
  iconObj: (state, getters) => {
    const list = getters.iconList;
    if (!list || list.length == 0) return null
    const obj = list.reduce((pre, cur) => {
      pre[cur.id] = cur;
      return pre;
    }, {})
    return obj;
  },
  selectTabIconList: (state, getters) => {
    const list = getters.iconList;
    if (getters.selectTab == null) return [];
    return list.filter(t => t.landmarkCat_id == getters.selectTab.id)
  }
}

const mutations = {
  setFeatureList(state, list) {
    state.featureList = list;
  },
  setFeatures(state, list) {
    state.features = list;
  },
  setIconList(state, list) {
    state.iconList = list;
  },
  setActiveIcon(state, icon) {
    state.activeIcon = icon;
  },
  setEditFeature(state, feature) {
    state.editFeature = feature;
  },
  setMarkDatas(state, list) {
    state.markDatas = list;
  },
  putFeatureList(state, {
    feature,
    point
  }) {
    state.featureList = state.featureList.map(t => {
      if (t.id == feature.id) {
        return {
          ...t,
          marks: [point]
        }
      } else {
        return t;
      }
    })
  },
  deleteFeature(state, featureId) {
    state.featureList = state.featureList.map(t => {
      if (t.id == featureId) {
        return {
          ...t,
          marks: []
        }
      } else {
        return t;
      }
    })
  },
  delMarkData(state, data) {
    state.markDatas = state.markDatas.map(t => {
      if (t.id == data.id) {
        return {
          ...data,
          _del: true
        }
      } else {
        return t;
      }
    })
  },
  cancelDelMarkData(state, markId) {
    state.markDatas = state.markDatas.map(t => {
      if (t.dataID == markId) {
        return {
          ...t,
          _del: false
        }
      } else {
        return t;
      }
    })
  }
}

const actions = {
  // async getFeatures(context, data) {
  //   context.commit('setFeatures', []);
  //   let rst = await api.getFeatureList(data);
  //   return new Promise(resolve => {
  //     if (rst.success) {
  //       context.commit('setFeatures', rst.data)
  //     }
  //     resolve(rst);
  //   })
  // },
  async getIconList(context) {
    context.commit('setIconList', []);
    let rst = await api.getIconList({
      // landmarkCatID
    })
    return new Promise(resolve => {
      if (rst.success) {
        context.commit('setIconList', rst.data)
      }
      resolve(rst)
    })
  },
  async getMarkDatas(context) {
    let rst = await api.getMarkDatas();
    return new Promise(resolve => {
      if (rst.success) {
        let list = rst.data.map(t => {
          return {
            ...t,
            landmarkMark_type: t.dataType,
            _del: false
          }
        })
        context.commit('setMarkDatas', list)
      }
      resolve(rst)
    })
  },
  async getFeatureList(context) {
    context.commit('setFeatureList', [])
    let rst = await api.getFeatureList();
    return new Promise(resolve => {
      if (rst.success) {
        let list = rst.data.map(t => {
          return {
            ...t,
            landmarkMark_type: t.dataType,
          }
        })
        context.commit('setFeatureList', list)
      }
      resolve(rst)
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
